﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace CPlase.Internal.Commands
{

    public class ClearInstanceStorageCacheCommand : IRoleEchoCommand
    {
        public string LocalResourceName;
        public List<string> Pathes = new List<string>();

        public ClearInstanceStorageCacheCommand() { }

        public ClearInstanceStorageCacheCommand(string LocalResourceName)
        {
            this.LocalResourceName = LocalResourceName;
        }

        public ClearInstanceStorageCacheCommand(string LocalResourceName, string Path)
        {
            this.LocalResourceName = LocalResourceName;
            AddPath(Path);
        }

        public ClearInstanceStorageCacheCommand AddPath(string Path)
        {
            if (Pathes == null) Pathes = new List<string>();
            Pathes.Add(Path);
            return this;
        }

        public string Command
        {
            get { return this.GetType().AssemblyQualifiedName; }
        }

        public bool Execute()
        {
            foreach (var Path in Pathes)
            {
                InstanceStorage.Kill(LocalResourceName, Path);
            }
            return true;
        }

        public Dictionary<string, string> Serialize()
        {
            return new Dictionary<string, string>()
                {
                    {"pathes", JsonConvert.SerializeObject(Pathes)},
                    {"resource", LocalResourceName}
                };
        }

        public void Deserialize(NameValueCollection Params)
        {
            this.LocalResourceName = Params["resource"];
            this.Pathes = JsonConvert.DeserializeObject<List<string>>(Params["pathes"]);
        }

    }
}